﻿Shader "MicroMo/Micro标准材质无法线贴图" {
	Properties {
		_Color ("基本色", Color) = (1,1,1,1)
		_MainTex ("主颜色贴图", 2D) = "white" {}
	    _ColorBlendMain("主混合比例", Range(0,1)) = 1
	    _SubTex("副颜色贴图", 2D) = "white" {}
		_ColorBlendSub("副混合比例", Range(0,1)) = 1
	    _ColorBlendSubAreaStart("副裁剪程度开始", Range(0,3)) = 0.2
		_ColorBlendSubAreaEnd("副裁剪程度结束", Range(0,3)) = 0.2
		_Glossiness ("光滑程度", Range(0,1)) = 0.5
		_Metallic ("金属强度", Range(0,1)) = 0.0
	    _MaskColor("关键色", Color) = (1,1,1,1)
	}

	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200

		CGPROGRAM
	
		#pragma surface surf Standard addshadow fullforwardshadows vertex:vert

		#pragma target 3.0

		sampler2D _MainTex;
		sampler2D _SubTex;

		struct Input
		{
			float2 uv_MainTex;
			float2 uv_SubTex;
			half4 color;
		};

		half _Glossiness;
		half _Bumpness;
		half _Metallic;
		fixed4 _Color;
		fixed4 _MaskColor;
		half _ColorBlendMain;
		half _ColorBlendSub;
		half _ColorBlendSubAreaStart;
		half _ColorBlendSubAreaEnd;
		fixed4 ColorWhite = {1,1,1,1};

		void vert(inout appdata_full v, out Input o) 
		{
			UNITY_INITIALIZE_OUTPUT(Input, o);
			o.color = v.color;
		}

		void surf (Input IN, inout SurfaceOutputStandard o)
		{
			// Albedo comes from a texture tinted by color
			fixed4 c1 = tex2D (_MainTex, IN.uv_MainTex)  * _ColorBlendMain + (1- _ColorBlendMain)* _Color;
			fixed4 c2 = tex2D (_SubTex, IN.uv_SubTex);
			fixed4 c3;// = c2 * _ColorBlendSub + (1 - _ColorBlendSub)*c1;

			half c2lightness = c2.r + c2.g + c2.b;

			if (c2.a >= 0.99f)
			{
				float mak = _MaskColor.r * 256*256*256 + _MaskColor.g * 256*256 + _MaskColor.b*256;
				fixed4 diff = c2 - _MaskColor;

				//if (c2lightness > _ColorBlendSubAreaStart *3 && c2lightness < _ColorBlendSubAreaEnd * 3)
				if((diff.r + diff.g + diff.b) < -_ColorBlendSubAreaEnd || (diff.r+ diff.g+ diff.b) > _ColorBlendSubAreaStart)
				{
					//c3 = c2; // (1 - c2lightness)*c2 + c2lightness*c1;
					c3 = c2 * _ColorBlendSub + (1 - _ColorBlendSub)* c1;
				}
				else
				{
					c3 = c1;
				}
			}
			else
			{ 
				c3 = c2 * _ColorBlendSub * c1;
			}


			o.Albedo = c3.rgb * IN.color.rgb * 1;
			//Metallic and smoothness come from slider variables
			o.Metallic = _Metallic;
			
			o.Smoothness = _Glossiness;
			o.Alpha = c3.a;

		}

		float3 expand(float3 v) 
		{ 
			return (v - 0.5) * 2;
		}
		ENDCG
	}
	FallBack "Diffuse"
}
